package com.v2future.sjt.collegecheckingattendance.leave;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.v2future.sjt.collegecheckingattendance.MainActivity;
import com.v2future.sjt.collegecheckingattendance.R;
import com.v2future.sjt.collegecheckingattendance.common.tag.CacheIntents;
import com.v2future.sjt.collegecheckingattendance.utils.BaseResponseBean;
import com.v2future.sjt.collegecheckingattendance.utils.Constants;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.BaseRequestParams;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.NetTaskListener;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.NetUtils;

import org.xutils.x;

import java.io.File;
import java.util.List;

import me.nereo.multi_image_selector.MultiImageSelectorActivity;

/**
 * Created by minnie on 16/1/19.
 * 上传假单
 */
public class UpLoadPhotoActivity extends MainActivity implements View.OnClickListener{

    LinearLayout fromPhotoLayout, takePhotoLayout;

    ImageView picIv;

    Button submitBtn;

    String uploadFilePath = null;

    int REQUEST_IMAGE = 1001;


    @Override
    public void init() {
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_upload_photo);
        initTitleBack(null);
        setTitleText("");
        fromPhotoLayout = (LinearLayout) findViewById(R.id.layout_from_photos);
        fromPhotoLayout.setOnClickListener(this);
        takePhotoLayout = (LinearLayout) findViewById(R.id.layout_take_photo);
        takePhotoLayout.setOnClickListener(this);
        picIv = (ImageView) findViewById(R.id.iv_pic);
        submitBtn = (Button) findViewById(R.id.btn_submit);
        submitBtn.setOnClickListener(this);
    }

    @Override
    public void initAfter() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == REQUEST_IMAGE){
            if(resultCode == RESULT_OK){
                // Get the result list of select image paths
                List<String> path = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
                if (path.size() > 0 ) {
                    x.image().bind(picIv, path.get(0).toString(), getImageOptions());
                    uploadFilePath = path.get(0).toString();
                }
            }
        }
    }

    public View.OnClickListener backListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_from_photos:

//                break;
            case R.id.layout_take_photo:
                Intent intent = new Intent(UpLoadPhotoActivity.this, MultiImageSelectorActivity.class);
                // whether show camera
                intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, true);
                // max select image amount
                intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, 1);
                // select mode (MultiImageSelectorActivity.MODE_SINGLE OR MultiImageSelectorActivity.MODE_MULTI)
                intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_SINGLE);
                // default select images (support array list)
//                intent.putStringArrayListExtra(MultiImageSelectorActivity.EXTRA_DEFAULT_SELECTED_LIST, defaultDataArray);
                startActivityForResult(intent, REQUEST_IMAGE);
                break;
            case R.id.btn_submit:
                if (TextUtils.isEmpty(uploadFilePath)) {
                    showSimpleDialog("请上传请假凭证");
                    break;
                }
                String courseNumber = (String) getBaseApplication().getIntentData(CacheIntents.COURSELEAVE_COURSE_NUMBER);
                String date = (String) getBaseApplication().getIntentData(CacheIntents.COURSELEAVE_DATE);
                if (TextUtils.isEmpty(courseNumber) || TextUtils.isEmpty(date)) {
                    showSimpleDialog("请假信息有误");
                    break;
                }
                showLoadingDialog();
                doSubmitCourseLeave(courseNumber, date);
                break;
            default:
                break;
        }
    }

    private void doSubmitCourseLeave(final String courseNumber, final String date) {
        BaseRequestParams params = new BaseRequestParams(this, formatUrlDo(Constants.PUBLISH_EVENT));
        params.setMultipart(true);
        params.addQueryStringParameter("type", "2");
        params.addQueryStringParameter("leaveType", "2");
        params.addQueryStringParameter("leaveDate", date);
        params.addQueryStringParameter("courseNumber", courseNumber);
        params.addQueryStringParameter("termId", "1");
        params.addQueryStringParameter("imageName0", urlEncode(uploadFilePath));
        params.addBodyParameter("imageBody0", new File(uploadFilePath), null);
        NetUtils.doPostRequestWithFiles(this, params, new NetTaskListener<BaseResponseBean>() {

            /**
             * 数据获取完成
             *
             * @param result
             */
            @Override
            public void onSuccess(BaseResponseBean result) {
                showOneBtnDialog(getString(R.string.notice), "请假申请已提交", getString(R.string.iknow), new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        HideDialog();
                        finish();
                    }
                });
            }

            /**
             * 位置网络错误
             */
            @Override
            public void onError() {

            }

            /**
             * 非正常状态的数据
             *
             * @param result
             */
            @Override
            public void onInterError(BaseResponseBean result) {
                tip(result.getFailReason());
            }

            /**
             * 网络错误
             *
             * @param responseCode
             * @param responseMsg
             * @param errorResult
             */
            @Override
            public void onNetError(int responseCode, String responseMsg, String errorResult) {
                tip(responseMsg);
            }

            /**
             * 请求取消
             */
            @Override
            public void onCancelled() {

            }

            /**
             * 网络请求流程结束
             */
            @Override
            public void onFinished() {
                cancelLoadingDialog();
            }

            /**
             * 缓存获取完成
             *
             * @param result
             */
            @Override
            public void onSuccessCache(BaseResponseBean result) {

            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
